<?php
/**
 * Created by PhpStorm.
 * User: helly
 * Date: 14-11-18
 * Time: 17:15
 */

namespace Admins\Model;

use Common\Model\Model;

class UserModel extends  Model{

    //自动验证
    protected $_validate = array(
        //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
        array('uname', 'require', '用户名必须填写!'),
        array('umobile','/1[3458]{1}\d{9}$/','手机号码不正确',2,'regex'),
        array('status', array(0, 1), '状态值的范围不正确！', 2, 'in'),
        array('uemail', '', '邮箱已经存在！', 2, 'unique', 3),
        array('umobile', '', '手机已经存在！', 2, 'unique', 3),
    );

    //array(填充字段,填充内容,[填充条件,附加规则])
    protected $_auto = array(
        array('create_time', 'time', 1, 'function'),
        array('authkey', 'genRandomString', 1, 'function', 6), //新增时自动生成验证码
        array('status', '1'),
        array('logtimes','0'),
    );


    /**
     * 添加会员操作
     * @param type $data
     * @return boolean
     */
    public function createUser($data) {
        if (empty($data)) {
            $this->error = '没有数据！';
            return false;
        }
        $data['upassword']=!empty($data['upassword'])?trim($data['upassword']):'admins123';
        if ($this->create($data)) {
            $userid = $this->add();
            if ($userid) {
                if ($data['uavatar']) {
                    service("Attachment")->api_update('', 'userid-ua' . $userid, 1);
                }
                if ($data['ubackground']) {
                    service("Attachment")->api_update('', 'userid-ua' . $userid, 1);
                }
                return $userid;
            }
            $this->error = '会员添加失败！';
            return false;
        } else {
            return false;
        }
    }

    /**
     * 修改会员信息
     * @param $data
     * @return boolean
     */
    public function editUser($data){
        if (empty($data) || !is_array($data) || !isset($data['userid'])) {
            $this->error = '没有需要修改的数据！';
            return false;
        }
        $info = $this->where(array('userid' => $data['userid']))->find();
        if (empty($info)) {
            $this->error = '该会员信息不存在！';
            return false;
        }
        //密码为空，表示不修改密码
        if (isset($data['upassword']) && empty($data['upassword'])) {
            unset($data['upassword']);
        }
        if ($this->create($data)) {
            if ($this->data['upassword']) {
                $this->authkey = genRandomString(6);
                $this->upassword = $this->hashPassword($this->upassword, $this->authkey);
            }
            if($info['ifrz']==false && $data['ifrz']==true){
                $today=date('Y年m月d日',time());
                insertSysmsg($info['userid'],'获嘉您升级为烘焙师','恭喜您于'.$today.'申请认证烘焙师已审核通过，继续努力哦！');
            }

            $status = $this->save();
            if(false!==$status){
                if ($data['uavatar']) {
                    service("Attachment")->api_update('', 'userid-ua' . $data['userid'], 1);
                }
                if ($data['ubackground']) {
                    service("Attachment")->api_update('', 'userid-ua' . $data['userid'], 1);
                }
            }
            return $status !== false ? true : false;
        }
        return false;
    }

    /**
     * 删除会员
     * @param $data
     * @return bool
     */
    public function deleteUser($data){
        if(empty($data)){
            $this->error='未找到会员信息';
            return false;
        }
        $where['userid']=$data;
        if($this->where($where)->delete()!==false){
            service("Attachment")->api_delete('userid-ua' . $data);
            return true;
        }else{
            $this->error='会员删除失败';
            return false;
        }
    }


    /**
     * 对明文密码，进行加密，返回加密后的密文密码
     * @param string $password 明文密码
     * @param string $authkey 认证码
     * @return string 密文密码
     */
    public function hashPassword($password, $authkey = "") {
        return md5($password . md5($authkey));
    }

    /**
     * 插入成功后的回调方法
     * @param type $data 数据
     * @param type $options 表达式
     */
    protected function _after_insert($data, $options) {
        //添加信息后，更新密码字段
        $this->where(array('userid' => $data['userid']))->save(array(
            'upassword' => $this->hashPassword($data['upassword'], $data['authkey']),'openuid'=>'10000'+$data['userid']
        ));
    }

    /*
     * 添加回复数据
     * yf 0916
     */
    public function sendMessage($post,$type){
    	$model=M('UserMessage');
    	if(empty($post)){
    		$this->error='提交数据出错,请重试!';
    		return false;
    	}
    	$post['type']=$type;
    	$post['create_time']=time();
    	$post['status']=true;
    	if ($model->create($post)) {
    		$result = $model->add();
    		if ($result) {
    			return true;
    		}else{
    			$this->error = '留言失败,请稍候再试！';
    			return false;
    		}
    	} else {
    		return false;
    	}
    }

}
